package com.eshop.message.redis.config;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Iterator;
import java.util.List;
import com.eshop.modules.shop.domain.SystemConfig;
import com.eshop.utils.RedisUtils;
import com.eshop.modules.shop.service.SystemConfigService;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.boot.CommandLineRunner;

/**
 * 容器初始化完成后，自动调用CommandLineRunner中的run方法
 * 不同的CommandLineRunner实现可以通过@Order()指定执行顺序
 */
@Component
public class RedisKeyInitialization implements CommandLineRunner
{
    private static final Logger log;
    private final SystemConfigService systemConfigService;
    private final RedisUtils redisUtils;

    //redis初始化
    private void redisKeyInitialization() {
        try {
            final List<SystemConfig> systemConfigs = (List<SystemConfig>)this.systemConfigService.list();
            for (final SystemConfig systemConfig : systemConfigs) {
                this.redisUtils.set(systemConfig.getMenuName(), (Object)systemConfig.getValue());
            }
            RedisKeyInitialization.log.info("---------------redisKey初始化成功---------------");
        }
        catch (Exception e) {
            RedisKeyInitialization.log.error("redisKey初始化失败: {}", (Object)e.getMessage());
        }
    }
    
    public void run(final String... args) throws Exception {
        this.redisKeyInitialization();
    }
    
    @Autowired
    public RedisKeyInitialization(final SystemConfigService systemConfigService, final RedisUtils redisUtils) {
        this.systemConfigService = systemConfigService;
        this.redisUtils = redisUtils;
    }
    
    static {
        log = LoggerFactory.getLogger((Class)RedisKeyInitialization.class);
    }
}
